Skip to content

Conversation

@dariatiurina
Copy link
Contributor

@dariatiurina dariatiurina commented Nov 25, 2025

Fix empty render of the ErrorBoundary

Description

This PR fixes a bug where ErrorBoundary renders empty content instead of ErrorContent when multiple child components throw errors in quick succession (e.g., errors thrown inside a foreach loop like in the test example).

How it works normally (single error):

The Renderer queues an empty render to clear the subtree, then calls HandleException, which queues the ErrorContent render. This works correctly.

The bug (multiple errors):

When a second error is thrown:

  1. Renderer queues another empty render - this sets _hasPendingQueuedRender = true
  2. Renderer calls HandleException to queue the ErrorContent render
  3. StateHasChanged() returns early because _hasPendingQueuedRender is still true from step 1
  4. The ErrorContent render is never queued

The queue ends up with: [empty, ErrorContent, empty] instead of [empty, ErrorContent, empty, ErrorContent]. After the first ErrorContent renders, only the empty render remains in the queue, clearing the error UI.

Solution:

We already use path ComponentBase -> RenderHandle -> Renderer. By adding new _componentsAllowNextRender hash set we now can queue components that need to render with ignoring _hasPendingQueuedRender flag.

Fixes #56950

@github-actions github-actions bot added the area-blazor Includes: Blazor, Razor Components label Nov 25, 2025
@dariatiurina dariatiurina self-assigned this Nov 25, 2025
@ilonatommy
Copy link
Member

Could we add some unit tests that would check scenarios like:
– Error -> non‑error state change -> still shows error
– multiple errors with a non‑error render in between
– a non‑error StateHasChanged that itself schedules another render right away (for example from inside the render or lifecycle), between two errors, to verify which render is allowed past _hasPendingQueuedRender and that the second error still shows the error UI?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Blazor ErrorBoundary error content not shown when exception occurs inside @foreach

3 participants